<?php
require_once 'common.inc.php';
!in_array('2', $larr) && adminmsg('你没有被赋予操作该项的权限！');

$matches = parse_url($_SERVER['HTTP_REFERER']);

foreach($_POST as $_key => $_value) {
	!ereg("^\_", $_key) && $$_key = $_POST[$_key];
} 
foreach($_GET as $_key => $_value) {
	!ereg("^\_", $_key) && $$_key = $_GET[$_key];
} 

if ('list' == $action) {
	$limit = 10;
	$page = intval($page) < 1?1:intval($page);
	$start = (intval($page)-1) * $limit;
	$sql = "select count(*) from ps_ad";
	$query = $dp -> query($sql);
	$count = $dp -> fetch_array($query); 
	// print $count[0];
	$allpage = ceil($count['count(*)'] / $limit);
	$page = $page > $allpage?$allpage:$page;
	$url = 'ad.php?action=' . $action . '&amp;';
	$pages = numofpage($count['count(*)'], $page, $allpage, $url);

	$sql = "select * from ps_ad limit $start,$limit";
	$query = $dp -> query($sql);
	while ($resulte = $dp -> fetch_array($query)) {
		$adlist[] = $resulte;
	} 

	include_once MCC_ROOT . '/templates/msystem/adlist.htm';
} elseif ('edit' == $action) {
	$action = 'oedit';
	$sql = "select * from ps_ad where id='$id'";
	$query = $dp -> query($sql);
	while ($resulte = $dp -> fetch_array($query)) {
		$adlist = $resulte;
	} 
	include_once MCC_ROOT . '/templates/msystem/adadd.htm';
} elseif ('oedit' == $action) {
	if ('-1' == $position) {
		adminmsg('请选择广告位置', 'ad.php?action=list');
	} else {
		$sql = "update ps_ad set title='$title',content='$content',position='$position' where id='$id'";
		$query = $dp -> query($sql);

		adminmsg('修改成功', 'ad.php?action=list');
	} 
} elseif ('delete' == $action) {
	$sql = "delete from ps_ad where id='$id'";
	$query = $dp -> query($sql);
	adminmsg('删除成功', 'ad.php?action=list');
} elseif ('add' == $action) {
	$action = 'oadd';
	include_once MCC_ROOT . '/templates/msystem/adadd.htm';
} elseif ('oadd' == $action) {
	if ('-1' == $position) {
		adminmsg('请选择广告位置', 'ad.php?action=list');
	} else {
		$sql = "insert into ps_ad (title,content,position) values ('$title','$content','$position')";
		$query = $dp -> query($sql);
		adminmsg('添加成功', 'ad.php?action=list');
	} 
} elseif ('switch' == $action) {
	$sql = "update ps_ad set enable='0'";
	$query = $dp -> query($sql);

	$sql = "update ps_ad set enable='1' where ";
	foreach($_POST as $key => $value) {
		if (1 == $value)
			$sql .= " id = '$key' or ";
	} 
	$sql .= " id = '0' ";
	$query = $dp -> query($sql);

	$sql="select * from ps_ad where enable='1'";
	$query=$dp->query($sql);
	while($result=$dp->fetch_array($query)){
		$list[]=$result;
	}
	foreach($list as $value){
		$key=$value[position];
		$aaa[$key]=$value[content];		
	}
	$bbb="<?php\n";
	for($i=0;$i<6;$i++){
		if($aaa[$i]){
			$bbb.="\$ad$i='".$aaa[$i]."';\n";
		}else{
			$bbb.="\$ad$i='';\n";
		}
	}
	$bbb.="?>";

	$filename="../push/log/ad.php";
		$fp=fopen($filename,'wb+');
		if($fp){
			file_put_contents($filename,$bbb);
		}else{
			adminmsg('更新缓存失败',"ad.php?action=list");
		}
	
	adminmsg('修改成功', 'ad.php?action=list');
} 


?>